<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        li {
            list-style: none;
        }

        .nav {
            width: 100vw;
            height: 10vw;
            overflow: hidden;
            border-bottom: 1px solid #eee;
        }

        .nav__wrapper {
            display: inline-flex;
            height: 10vw;
        }

        .nav__item {
            width: 20vw;
            text-align: center;
            line-height: 10vw;
        }

        .nav__item.active {
            border-bottom: 3px solid orange;
        }

        .content {
            overflow: hidden;
            width: 100vw;
            height: calc(100vh - 10vw);
        }

        .content__wrapper {
            display: inline-flex;
        }

        .content__item {
            width: 100vw;
            font-size: 30px;
            text-align: center;
            padding-top: 200px;
            height: 200vh;
        }

        .content__item:nth-child(2n + 1) {
            background-color: pink;
        }

        .content__item:nth-child(2n + 2) {
            background-color: lightblue;
        }

        .content__item:nth-child(2n + 3) {
            background-color: violet;
        }

        .content__item:nth-child(2n + 4) {
            background-color: orange;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="nav">
            <ul class="nav__wrapper">
                <li class="nav__item active">新闻</li>
                <li class="nav__item">体育</li>
                <li class="nav__item">社会</li>
                <li class="nav__item">财经</li>
                <li class="nav__item">娱乐</li>
                <li class="nav__item">汽车</li>
                <li class="nav__item">游戏</li>
            </ul>
        </div>
        <div class="content">
            <div class="content__wrapper">
                <div class="content__item">新闻</div>
                <div class="content__item">体育</div>
                <div class="content__item">社会</div>
                <div class="content__item">财经</div>
                <div class="content__item">娱乐</div>
                <div class="content__item">汽车</div>
                <div class="content__item">游戏</div>
            </div>
        </div>
    </div>
    <script src="https://unpkg.com/better-scroll@latest/dist/better-scroll.js"></script>
    <script>
        var navBs = BetterScroll.createBScroll(".nav", {
            scrollX: true,
            click: true,
            bounce: false,
        });
        var contentBs = BetterScroll.createBScroll(".content", {
            scrollX: true,
            scrollY: true,
            click: true,
            momentum: false,
            // bounce: {
            //     top: true,
            //     bottom: false,
            //     left: false,
            //     right: false,
            // },
            bounce: true,
        });

        var navItems = document.querySelectorAll(".nav__item");
        var contentItems = document.querySelectorAll(".content__item");

        var contentItemWidth = contentItems[0].offsetWidth;
        var currentIndex = 0;

        function navItemActive(activeIndex) {
            for (var i = 0; i < navItems.length; i++) {
                navItems[i].classList.remove("active");
            }
            navItems[activeIndex].classList.add("active")
        }

        for (var i = 0; i < navItems.length; i++) {

            //先给每个navItem  添加一个自定义属性 data-index
            navItems[i].setAttribute("data-index", i);

            //为每个navItem绑定点击事件
            navItems[i].addEventListener('click', function () {

                //再点击事件回调函数中 获取当前被点击的元素index值
                var index = this.getAttribute("data-index");

                // 根据这个index值 找到对应的contentItem元素，并且滚动过去
                contentBs.scrollToElement(contentItems[index], 300);

                // 也让navItem滚动到自己的地方
                navBs.scrollToElement(navItems[index], 300, -navItems[0].offsetWidth);

                // navItem的排他选中效果
                navItemActive(index);

                // 设置currentIndex 为当前选中的index
                currentIndex = index;
            });
        }

        var currentIndex = 0;
        var contentWidth = contentItems[0].offsetWidth;

        // 利用节流阀的思想实现 滚动事件的响应限制
        var flag = false;
        contentBs.on('scrollEnd', function (e) {

            if (flag) {
                console.log('---');
                return;
            }
            console.log('=====');
            // 每次执行事件处理的代码之前， 把节流阀给关上，设置flag为true
            flag = true;
            console.log(e.x);
            if (Math.abs(e.x) % contentWidth > contentWidth / 4) {
                currentIndex++
            }
            contentBs.scrollTo(-currentIndex * contentWidth, e.y, 300)

            setTimeout(function () {
                flag = false;
            }, 350);
        })
    </script>
</body>

</html>